package com.ewhale.feitengguest.ui.user.message.chat.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.Message;
import com.hyphenate.helpdesk.callback.Callback;
import com.hyphenate.helpdesk.easeui.adapter.MessageAdapter;
import com.hyphenate.helpdesk.easeui.widget.chatrow.ChatRow;
import com.hyphenate.helpdesk.model.MessageHelper;
import com.hyphenate.helpdesk.model.OrderInfo;
import com.simga.library.utils.glide.GlideUtil;

public class ChatRowOrder extends ChatRow {
    ImageView mImageView;
    TextView mTextViewDes;
    TextView mTextViewprice;
    TextView mChatTextView;

    public ChatRowOrder(Context context, Message message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == Message.Direct.RECEIVE ? com.hyphenate.helpdesk.R.layout.hd_row_received_message : com.hyphenate.helpdesk.R.layout.em_row_sent_order, this);
    }

    @Override
    protected void onFindViewById() {
        mTextViewDes = (TextView) findViewById(com.hyphenate.helpdesk.R.id.tv_description);
        mTextViewprice = (TextView) findViewById(com.hyphenate.helpdesk.R.id.tv_price);
        mImageView = (ImageView) findViewById(com.hyphenate.helpdesk.R.id.iv_picture);
        mChatTextView = (TextView) findViewById(com.hyphenate.helpdesk.R.id.tv_chatcontent);
    }

    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        if (message.direct() == Message.Direct.RECEIVE) {
            //设置内容
            mChatTextView.setText(txtBody.getMessage());
            //设置长按事件监听
            mChatTextView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
//                    activity.startActivityForResult(new Intent(activity, ContextMenuActivity.class)
//                            .putExtra("position", position)
//                            .putExtra("type", Message.Type.TXT.ordinal()), CustomChatFragment.REQUEST_CODE_CONTEXT_MENU);
                    return true;
                }
            });
            return;
        }
        OrderInfo orderInfo = MessageHelper.getOrderInfo(message);
        if (orderInfo == null) {
            return;
        }
        mTextViewDes.setText(orderInfo.getTitle());
        mTextViewprice.setText(orderInfo.getPrice());
        String imageUrl = orderInfo.getImageUrl();
        if (!TextUtils.isEmpty(imageUrl)) {
            GlideUtil.loadPicture(imageUrl,mImageView,com.hyphenate.helpdesk.R.drawable.hd_default_image);
        }

        message.setMessageStatusCallback(new Callback() {
            @Override
            public void onSuccess() {
//                ChatClient.getInstance().chatManager().getConversation(message.getTo()).removeMessage(message.getMsgId());
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (adapter instanceof MessageAdapter) {
                            ((MessageAdapter) adapter).refresh();
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                Log.e(TAG, "error:" + s);
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }

    @Override
    protected void onBubbleClick() {

    }

}
